const express = require("express");
const app = new express();

let usernames = [];

app.use(express.urlencoded({
    extended: true
}))

//解析JSON格式请求体的中间件
app.use(express.json());

app.use((req, res, next) => {
    // 设置响应头
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Methods", "POST");
    res.setHeader("Access-Control-Allow-Headers", "Content-type");
    next();
})

app.post('/test', (req, res) => {
    const username = req.body.username;
    if (!usernames.filter(item => item === username).length) {
        res.send({
            status:true
        })
    } else {
        res.send({
            status:false
        })
    }
})

app.post('/user', (req, res) => {
    const username = req.body.username;
    // 如果不存在相同则添加，存在相同就不添加
    if (!usernames.filter(item => item === username).length) {
        usernames.push(username);
        res.send({
            status:true
        })
    } else {
        res.send({
            status:false
        })
    }
})

app.listen(3000, () => {
    console.log("服务器已启动");    
})